/*  Copyright (C) 2012 Philip Smith
 *
 *  This file is part of SuperChat.
 *
 *  SuperChat is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SuperChat is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with SuperChat.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.supertycoon.bukkit.superchat;

import java.util.HashSet;
import java.util.LinkedList;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

public class ChatRoom {
	HashSet<CommandSender> players;
	String name;
	String password;
	boolean modroom;
	
	public ChatRoom(String name, String password, boolean modroom) {
		this.players = new HashSet<CommandSender>();
		this.name = name;
		this.password = password;
		this.modroom = modroom;
	}
	
	void chat(String message) {
		for (CommandSender cs : this.players)
			SuperChat.players.get(cs).dispatch(this, message);
		@SuppressWarnings("unchecked")
		HashSet<CommandSender> temp = ((HashSet<CommandSender>) SuperChat.monitors.clone());
		temp.removeAll(this.players);
		for (CommandSender cs : temp) {
			SuperChat.players.get(cs).dispatch(this, ChatColor.GREEN + "*" + message);
		}
	}
	
	void addPlayer(CommandSender player) {
		chat(ChatColor.GOLD.toString() + player.getName() + " joined");
		this.players.add(player);
	}
	
	void removePlayer(CommandSender player) {
		this.players.remove(player);
		chat(ChatColor.DARK_GRAY.toString() + player.getName() + " quit");
		if (this.players.isEmpty())
			SuperChat.rooms.remove(this.name);
	}
	
	void close() {
		chat(ChatColor.RED.toString() + "Chat room is being closed");
		for (CommandSender player : this.players)
			SuperChat.players.get(player).removeChatRoom(this);
		SuperChat.rooms.remove(this.name);
	}

}
